package com.wee.aircoach_user;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigtotoro.util.SharedPreferencesUtil;
import com.bigtotoro.widgets.AppProgressBar;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wee.entity.MD5Util;
import com.wee.entity.Point;
import com.wee.entity.ShareWithTest;
import com.wee.entity.Sharedata;

import org.json.JSONException;
import org.json.JSONObject;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

/**
 * Created by Solomon on 2015/12/25.
 */
public class MyTestPlanActivity extends Activity implements View.OnClickListener{

    private String test0,test1,test2,test3,all,test4,test5,test6,name,gender;
    private TextView t0,t1,t2,t3,zong,other;
    private int ss,exact;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_plan);
        exact=  SharedPreferencesUtil.getInt(this, com.wee.aircoach_user.Constant.USERTIME);
        initView();
        post_json();
    }

    private void initView() {
        t0= (TextView) findViewById(R.id.test0);
        t1= (TextView) findViewById(R.id.test1);
        t2= (TextView) findViewById(R.id.test2);
        t3= (TextView) findViewById(R.id.next);
        zong= (TextView) findViewById(R.id.defen);
        other=(TextView) findViewById(R.id.other);
        findViewById(R.id.share).setOnClickListener(this);
        findViewById(R.id.again).setOnClickListener(this);
        findViewById(R.id.finish).setOnClickListener(this);
        name= getIntent().getStringExtra("name");
        test0= getIntent().getStringExtra("Heart");
        test1= getIntent().getStringExtra("Breath");
        test2= getIntent().getStringExtra("Strenth");
        test3= getIntent().getStringExtra("Percent");
        all= getIntent().getStringExtra("Score");
        test4= getIntent().getStringExtra("He");
        test5= getIntent().getStringExtra("Br");
        test6= getIntent().getStringExtra("St");
        ss=  SharedPreferencesUtil.getInt(MyTestPlanActivity.this, Constant.USERID);
        ImageView imageView = (ImageView)findViewById(R.id.bg_iv);
        ImageLoader.getInstance().displayImage("drawable://" + R.drawable.dialog_bg, imageView);
        gender=SharedPreferencesUtil.get(this, Constant.GENDER);
        if(gender.equals("male")){
            other.setText("俯卧撑");
        }else{
            other.setText("仰卧起坐");
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.share:
                shareWith();
                break;
            case R.id.again:
              /*  startActivity(new Intent(MyTestPlanActivity.this,PhysicalTestActivity.class));*/
                startActivity(new Intent(MyTestPlanActivity.this, MainActivity.class));
                finish();
                break;
            case R.id.finish:
                startActivity(new Intent(MyTestPlanActivity.this, MainActivity.class));
                finish();
                break;
        }
    }

    private void showShare(String  url) {
        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

// 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle(name+"的体测结果为"+all+"分,完胜"+test3+"用户");
        oks.setUrl(url);
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
//        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("光合教练—真人教练在线定制健身服务");

   //     oks.setTitleUrl("http://www.mob.com");
        oks.setImageUrl("http://image-test.aircoach.cn/media/340ef818fafa831ee270dadac0f0e39a.png");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
    /*    // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");*/

// 启动分享GUI
        oks.show(this);
    }
     private void  shareWith(){
         ShareWithTest content1 = new ShareWithTest();
         content1.setScore(all);
         content1.setBreath(test1);
         content1.setHeart(test0);
         content1.setStrenth(test2);
         content1.setBreath_score(test5);
         content1.setHeart_score(test4);
         content1.setStrenth_score(test6);
         Gson gson = new Gson();
         String json = gson.toJson(content1);
         RequestParams params = new RequestParams();
         AppProgressBar.checkAndCreateProgressBar(this);
         params.addBodyParameter("userId",""+ss);
         params.addBodyParameter("token", ""+ MD5Util.md5(exact));
         params.addBodyParameter("type", "test");
         params.addBodyParameter("content", json);
         HttpUtils http = new HttpUtils();
         http.send(HttpRequest.HttpMethod.POST,
                 //改动借口
                 Constant.USER_SHARE,
                 params,
                 new RequestCallBack<String>() {

                     @Override
                     public void onStart() {
                     }
                     @Override
                     public void onLoading(long total, long current, boolean isUploading) {

                     }
                     @Override
                     public void onSuccess(ResponseInfo<String> responseInfo) {
                         String str = responseInfo.result;
                             Gson gs=new Gson();
                             JsonObject jo=gs.fromJson(str, JsonObject.class);
                             JsonElement je=jo.get("status");
                             String status = je.getAsString();
                             if("ok".equals(status)){
                                 JsonObject object = jo.getAsJsonObject("data");
                                 Sharedata list1 =gs.fromJson(object, Sharedata.class);
                                 showShare(list1.getUrl());
                                 AppProgressBar.closeProgressBar();
                                 //   share(list1.getUrl());
                             }
                             AppProgressBar.closeProgressBar();

                     }

                     @Override
                     public void onFailure(HttpException error, String msg) {
                         AppProgressBar.closeProgressBar();
                     }
                 });
     }


    private void post_json() {

        RequestParams params = new RequestParams();
        t0.setText(test0.toString().trim());
        t1.setText(test1.toString().trim());
        t2.setText(test2.toString().trim());
        t3.setText(test3.toString().trim());
        zong.setText(all.toString().trim() );

        Point content = new Point();
        content.setScore(all);
        content.setBreath(test1);
        content.setHeart(test0);
        content.setStrenth(test2);
        content.setPercent(test3);
        Gson gson = new Gson();
        String json = gson.toJson(content);
        params.addBodyParameter("userId", ss+"");
        params.addBodyParameter("token", ""+ MD5Util.md5(exact));
        params.addBodyParameter("content", json);
        params.addBodyParameter("type", "test");

        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST,
                Constant.TICE_HISTORY,
                params,
                new RequestCallBack<String>() {

                    @Override
                    public void onStart() {
                    }
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {

                    }
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String str = responseInfo.result;
                        JSONObject json;
                        try {
                            json = new JSONObject(str);
                            if(json.getString("status").equals("ok")){
                                int userid = json.getInt("data");
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {

                    }
                });
    }
}
